package com.zhuoyue.biz.core.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.dao.GatewayDao;
import com.zhuoyue.biz.core.api.dao.GroupMapper;
import com.zhuoyue.biz.core.api.domain.*;
import com.zhuoyue.biz.core.api.service.GatewayRedisService;
import com.zhuoyue.biz.core.command.impl.ShuangSeWen0Faa0D02;
import com.zhuoyue.common.utils.CmdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.biz.core.api.dao.GroupQuickMapper;
import com.zhuoyue.biz.core.api.service.GroupQuickService;
/**
 * @ClassName: GroupQuickServiceImpl
 * @Description:
 * @author wjming
 * @Date 2021-07-14 20:19
 */
@Service
public class GroupQuickServiceImpl extends ServiceImpl<GroupQuickMapper, GroupQuick> implements GroupQuickService {
    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private GroupQuickMapper groupQuickMapper;

    @Autowired
    private GatewayDao gatewayDao;

    @Autowired
    private GatewayRedisService gatewayRedisService;


    @Override
    public void dealQuick(String id) throws ParseException {
        GroupQuick groupQuick = groupQuickMapper.selectById(id);
        Group group = groupMapper.selectById(groupQuick.getGroupId());
        GatewayDO gatewayDO = gatewayDao.selectOne(Wrappers.<GatewayDO>lambdaQuery().eq(GatewayDO::getHomeId, group.getHomeId()));
        Map<String, Object> sswMap = new HashMap<>(5);
        Map<String, Object> action = new HashMap<>(5);
        if (groupQuick.getDuration() != null) {
            action.put("duration", groupQuick.getDuration() + "");
        }
        if (groupQuick.getColorTemperature() != null) {
            action.put("setColorTemperature", groupQuick.getColorTemperature() + "");
        }
        if (groupQuick.getBrightness() != null) {
            action.put("setBrightness", groupQuick.getBrightness() + "");
        }
        sswMap.put("actions", JSONObject.toJSONString(action));
        String hexCode = CmdUtils.getHighPositionReplacement(CmdUtils.getHexString(Integer.parseInt(group.getCode())));
        String cmd = ShuangSeWen0Faa0D02.getCommand(sswMap, gatewayDO.getZigbeeMac(), hexCode, null, true);
        System.out.printf(cmd);
        // 发送指令
        gatewayRedisService.writeDataToGateway(gatewayDO.getZigbeeMac(), cmd);
    }
}

